package com.sjgl.esdn.service.impl;

import com.sjgl.esdn.domain.TreeSelect;
import com.sjgl.esdn.domain.entity.Dept;
import com.sjgl.esdn.mapper.DeptMapper;
import com.sjgl.esdn.service.DeptService;
import com.sjgl.esdn.utils.SpringUtils;
import com.sjgl.esdn.utils.StringUtilsESDN;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 部门服务实现
 *
 * @author 缪恒铭
 * @date 2022/08/15
 */
@Service
public class DeptServiceImpl implements DeptService {
    private static final Logger log = LoggerFactory.getLogger(DeptServiceImpl.class);

    @Autowired
    DeptMapper deptMapper;

    @Override
    public String getDeptNameById(Long deptId) {
        return deptMapper.getDeptName(deptId);
    }

    @Override
    public Long getIdByDeptName(String deptName) {
        return deptMapper.getIdByDeptName(deptName);
    }

    @Override
    public Long getLeaderId(Long deptId) {
        return deptMapper.getLeaderId(deptId);
    }

    @Override
    public List<Long> getIdListByDeptNameList(List<String> deptNameList) {
        List<Long> list = new ArrayList<>();
        for (String deptName : deptNameList) {
            list.add(getIdByDeptName(deptName));
        }
        return list;
    }

    @Override
    public List<Long> getIdList() {
        return deptMapper.getIdList();
    }

    @Override
    public List<String> getDeptNameListByIdList(List<Long> deptIdList) {
        List<String> list = new ArrayList<>();
        for (Long deptId : deptIdList) {
            list.add(deptMapper.getDeptName(deptId));
        }
        return list;
    }

    @Override
    public List<String> getDeptNameList() {
        return deptMapper.getDeptNameList();
    }

    @Override
    public boolean checkDeptUnique(String deptName) {
        return deptMapper.checkDeptUnique(deptName) == 0;
    }

    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    @Override
    public List<Dept> selectDeptList(Dept dept)
    {
        return deptMapper.selectDeptList(dept);
    }

    /**
     * 查询部门树结构信息
     *
     * @param dept 部门信息
     * @return 部门树信息集合
     */
    @Override
    public List<TreeSelect> selectDeptTreeList(Dept dept)
    {
        List<Dept> depts = selectDeptList(dept);
        return buildDeptTreeSelect(depts);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<Dept> buildDeptTree(List<Dept> depts)
    {
        List<Dept> returnList = new ArrayList<Dept>();
        List<Long> tempList = new ArrayList<Long>();
        for (Dept dept : depts)
        {
            tempList.add(dept.getDeptId());
        }
        for (Dept dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<Dept> depts)
    {
        List<Dept> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 是否存在部门子节点
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Override
    public boolean hasChildByDeptId(Long deptId) {
        int result = deptMapper.hasChildByDeptId(deptId);
        return result > 0;
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<Dept> list, Dept t)
    {
        // 得到子节点列表
        List<Dept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Dept tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Dept> getChildList(List<Dept> list, Dept t)
    {
        List<Dept> tlist = new ArrayList<Dept>();
        Iterator<Dept> it = list.iterator();
        while (it.hasNext())
        {
            Dept n = (Dept) it.next();
            if (StringUtilsESDN.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Dept> list, Dept t)
    {
        return getChildList(list, t).size() > 0;
    }

}
